package ControlLayer;
import java.util.ArrayList;
import java.util.Scanner;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{

    //declare instance variables to reference containers
    public AddressBook adrsBook;
    private static AddressCtr addrCtr;
    public Person p;

    public AddressCtr()
    {
        //Constructor.
        adrsBook= new AddressBook();
    }

    public static AddressCtr getInstance()
    {
        //Creates a new instance if there is no instance running.
        if(addrCtr==null)
        {
            addrCtr= new AddressCtr();
        }
        return addrCtr;
    }

    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    public Person getPerson(long _id)
    {  //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        return adrsBook.findPerson(_id);
    }

    public boolean isPerson(long _id)
    {
        //Sees if a certain person exists in the container.
        boolean exist = false;
        if(getPerson(_id) != null)
        {
            exist = true;
        }
        return exist;
    }

    public String printPerson(Person _p)
    {   
        //Prints a list of information about a person.
        String str;
        str = " ID: " + _p.getID();
        str += "\n Name: " + _p.getname();
        str += "\n Address: " + _p.getaddress();
        str += ","+_p.getPostnr() + "," + _p.getcity();
        str += "\n Phone: " + _p.getTel() + "\n";
        return str;
    }

    public void createPerson(long _id, String _name, String _address, String _postalCode, String _city, String _phone)
    {   Person p=new Person(_id, _name, _address, _postalCode, _city, _phone);
        adrsBook.addPerson(p);
        //creates a Person object and stores it in the container
    }

    public void deletePerson(long _id)
    {
        Person tempPers=adrsBook.findPerson(_id);
        if(tempPers!=null)
            adrsBook.removePerson(tempPers);
        //search for the person and delete the object from the container
    }

    public String listAllPersons()
    { 
        //Prints a list of every person in the container.
        String str = " ";
        for(Person persons : adrsBook.person) 
        {  
            str += printPerson(persons);
        }
        return str;
    }
}